<template>
    <div style="height:100%">

        <el-alert
                title="题目添加表单"
                type="success">
        </el-alert>
        <br>
        <div class="cateform" style="width:50%">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                <el-form-item label="标题" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>

                <el-form-item label="音频地址" prop="ads">
                    <el-input v-model="ruleForm.ads"></el-input>
                </el-form-item>

                <el-form-item label="图片" prop="url">
                    <el-upload
                            class="upload-demo"
                            action="/admin/imageupload"
                            :on-remove="handleRemove"
                            :on-success = 'handleSuccess'
                            :file-list="fileList2"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <input type="hidden" v-model="ruleForm.url" prop="url">
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>

        </div>
    </div>
</template>

<script>

    export default {

        mounted(){

        },
        data() {
            return {
                isShow:false,
                ruleForm: {
                    name:'',
                    url:'',
                    ads:''
                },
                fileList: {
                    name: '',
                    url: ''
                },
                rules:{

                },
                fileList2:[]

            };
        },
        methods: {

            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post('/api/Mp3Add',this.ruleForm).then(rew=>{
                            if(rew.data==200){
                                this.open3();
                            }
                            if(rew.data==400){
                                this.open4();
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            open3() {
                this.$notify({
                    title: '添加成功',
                    message: '点击关闭',
                    type: 'success'
                });
            },
            open4() {
                this.$notify({
                    title: '图标，标志已存在',
                    message: '点击关闭',
                    type: 'error'
                });
            },
            handleRemove(file, fileList) {
                this.ruleForm.url = '';

            },
            change(file) {
                let data={
                    'id':file
                };
                axios.post('/GetImageLevel2',data).then(rew=>{
                    this.catelists2 = rew.data
                    if(this.catelists2 ==''){
                        this.isShow = false;
                        this.ruleForm.level2='';
                    }else{
                        this.isShow = true
                    }

                })
            },
            handleSuccess(file){
                this.ruleForm.url = file.id;
            },
        }
    }
</script>

